/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.
 */

package jdbcmanager.tree;

import java.awt.*;

import jdbcmanager.swing.*;
import jdbcmanager.app.*;
import jdbcmanager.dbtools.*;
import jdbcmanager.dbtools.datatypes.*;

/**
 * Tree nodes representing a table DB entity.
 *
 * @author Francisco Morero Peyrona
 */
final class NodeTable extends JdbcTreeNode
{
   private JFrameMain.JDBCManagerInternalFrame frame = null;
   
   NodeTable( String sCatalog, String sSchema )
   {
      super( icon_tree_table_node, new DbEntityDescriptor( sCatalog, sSchema, "Tables" ) );

      // Trick to force the tree to show the RootHandles (it will be removed before adding real nodes)
      add( new NodeCatalog( "" ) );
      setInvalidActions();
   }

   NodeTable( DbEntityDescriptor ded, boolean bTable )
   {
      super( bTable ? icon_tree_table : icon_tree_table_node, ded );

      setAllowsChildren( ! bTable );
      setInvalidActions();
   }

   boolean isInitialised()
   {
      return this.bWasInit || ! getAllowsChildren();
   }

   boolean new_()
   {
      DbEntityDescriptor ded = new DbEntityDescriptor( this.ded.getCatalog(), this.ded.getSchema(), "NoName" );
      
      return SqlUtil.createTable( DbUtil.getConnection( getCatalog() ), ded ) != null;
   }

   /**
    * Can be called only by one of the tables below "Table" node
    * @return
    */
   void open()
   {
      if( this.frame == null || this.frame.isClosed() )
      {         
         App.setCursor( Cursor.WAIT_CURSOR );
         App.getFrameMain().openSqlQuery( this.ded );
         App.restoreCursor();
      }
      else
      {
         this.frame.setSelected( true );
         this.frame.restore();
      }
   }

   /**
    * Can be called by one of the tables below "Tables" node
    * @return
    */
   boolean edit()
   {
      return SqlUtil.alterTable( DbUtil.getConnection( getCatalog() ), new TableDefinition( this.ded ) ) != null;
   }

   /**
    * 
    * @param sNewName
    * @return 
    */
   boolean rename( String sNewName )
   {
      boolean bSuccess = false;
      
      if( SqlUtil.renameTable( DbUtil.getConnection( getCatalog() ), new TableDefinition( this.ded ), sNewName ) )
      {
         bSuccess = true;
         
         if( this.frame != null )
            this.frame.rename( sNewName );   // Needed because serialize() saves internal DbEntityDescriptor
      }
      
      return bSuccess; 
   }

   /**
    * Can be called only by one of the tables below "Tables" node
    * @return
    */
   boolean delete()
   {
      // Can't close associated frame because the current SQL command can affect who knows which table      
      return SqlUtil.dropTable( DbUtil.getConnection( getCatalog() ), this.ded );
   }

   // Enabled only for each one of the tables (nodes that does not have childs): 
   // it does not make sense when highlighted node is the "Tables" node or one of the schemas or one of the table types node. 
   void info()
   {
      App.setCursor( Cursor.WAIT_CURSOR );
      String  sTitle = "Table '"+ getName() +"' in "+ DbUtil.getAgentDBMS().getAgentMetaData().getCatalogTerm() +" '"+ getCatalog() +'\''; 
      JxDialog dialog = new JxDialog( sTitle );
               dialog.getContentPane().add( new Info4Table( this.ded ) );
               dialog.pack();
               dialog.center();
               dialog.setVisible( true );
      App.restoreCursor();
   }
   
   private void setInvalidActions()
   {
   	  if( getAllowsChildren() )
      {
         setOpenable(   false );
         setEditable(   false );
         setDeleteable( false );
         setInfoable(   false );
      }
      else
      {
         setRefreshable( false );
      }
   }
}